const bodyParser = require('body-parser')
const { list } = require('./mock')
const JWT = require('jsonwebtoken')
module.exports = app => {
	app.use(bodyParser.json())

	app.get('/api/list', (req, res) => {
		const { pageCode = 1, pageSize = 10 } = req.query

		res.send({
			code: 200,
			data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
			total: list.length,
			hasMore: pageCode * pageSize < list.length
		})
	})


	app.get('/api/search', (req, res) => {
		const { value } = req.query

		const data = list.filter(v => v.title.includes(value))

		res.send({
			code: 200,
			data: data
		})

	})

	app.post('/api/login', (req, res) => {
		const { user, pwd } = req.body
		if (user === 'admin' && pwd === 'admin') {
			res.send({
				token: JWT.sign({ name: 'admin' }, '666666'),
				code: 200
			})
		} else {
			res.send({
				code: 403
			})
		}

	})


	app.get('/api/tag', (req, res) => {
		res.send({
			code: 200,
			data: ['查阅色', '关东煮', '吃饭', '唱歌']
		})
	})
}